package com.sczmgk.hotline.utils;

import android.content.Context;
import android.util.Log;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 将xml解析为PupWindow
 *
 */
public class XmlParser {

    private static final String PUPCONFIG_FILE_NAME = "pupwindow_config.xml";
    private static final Map<String, String> cacheMaps = new HashMap<String, String>();
    private static final String TAG = "Im";

    /**
     * 取得pupwindow条目的文字
     * @param ctx  上下文
     * @param id   弹出框的id 如： 拍照、选择相片
     * @return   如：  拍照/从手机相册选择
     */
    public static String getPupWindow(Context ctx, String id) {
        synchronized (cacheMaps) {
            String value = cacheMaps.get(id);
            if (value != null) {
                return value;
            }
        }
        String map = parseId2String(ctx, id);
        put2cache(id, map);
        return map;
    }
	/**
	 * 将Id转成String
     * xml  <pup id="6"> 拍照/从手机相册选择</pup>
	 * @param ctx
	 * @param id 节点Id
	 * @return    如传入  7 返回 拍照/从手机相册选择
	 */
    private static String parseId2String(Context ctx, String id) {
        String map = null;
        InputStream is = null;
        XmlPullParser parser = null;
        try {
//            is = ctx.getClassLoader().getResourceAsStream(PUPCONFIG_FILE_NAME);// ctx.openFileInput(PUPCONFIG_FILE_NAME);
            is = ctx.getClass().getClassLoader().getResourceAsStream("assets/"+PUPCONFIG_FILE_NAME);
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
            MyLog.d(TAG,"is = :"+is);
            parser.setInput(is, "UTF-8");
            if (null != parser) {
                String tagName = null;
                int eventType = parser.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_TAG) {
                        tagName = parser.getName();
                        if ("pup".equals(tagName)) {
                            if (parser.getAttributeValue(0).equalsIgnoreCase(id)) {
                                map = parser.nextText();
                                break;
                            }
                        }

                    }
                    eventType = parser.next();
                }
            }
        } catch (Exception e) {
            Log.w(TAG, "", e);
        } finally {
            close(is);
        }
        return map;
    }

    /**
     * 解析全部的Id(将id转成String并将其存入Map集合)
     * @param ctx
     */
    public static void parseAllId(Context ctx) {
        String map = null;
        InputStream is = null;
        XmlPullParser parser = null;
        try {
            is = ctx.getClassLoader().getResourceAsStream(PUPCONFIG_FILE_NAME);
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
            parser.setInput(is, "UTF-8");
            if (null != parser) {
                String tagName = null;
                String id = null;
                int eventType = parser.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_TAG) {
                        tagName = parser.getName();
                        if ("pup".equals(tagName)) {
                            id = parser.getAttributeValue(0);
                            map = parser.nextText();
                            put2cache(id, map);
                        }

                    }
                    eventType = parser.next();
                }
            }
        } catch (Exception e) {
            Log.w(TAG, "", e);
        } finally {
            close(is);
        }
    }

    /**
     * 将id和与之对应的String存入缓存Map集合
     * @param id  id 
     * @param map  与Id对应的String
     */
	private static void put2cache(String id, String map) {
        synchronized (cacheMaps) {
            if (map != null) {
                cacheMaps.put(id, map);
            }
        }
    }

    /**
     * 关闭输入流
     * @param is
     */
    private static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
